import React,{useState,useEffect} from 'react'
import TableSearch from './components/table_Search'
import Table from './components/table'
import {post } from '@/Utils/axios'
import { Button } from 'antd';
import '../activity/style/index.less'
import { history } from 'umi';
import {Url} from '@/Utils/services/url'
export default function activity_table(props:any) {
    const url = Url.activityList
    const [formItem,setFormItem] = useState({
        activityName:'',
        activityStatus:[],
        isDraft:0,
        page:1,
        pageSize:5,
    })
    const [data,setData] = useState([])
    const [needSearch,setNeedSearch] = useState(false)
    const [total,setTotal] = useState(0)
    useEffect(()=>{
        if(needSearch == true){
             handleSearchForm()
        }
    },[needSearch])
    useEffect(()=>{
        let obj = Object.assign({},formItem)
        props.type == 5?obj.activityStatus = []:props.type == 0?obj.activityStatus = []:obj.activityStatus.push(props.type)
        props.type == 5?obj.isDraft = 1:obj.isDraft = 0
        setFormItem(obj)
        setNeedSearch(true)
    },[props.type])
    function handleSearchForm(){
        post(url,formItem).then(res => {
            setTotal(res.total)
            setData(res.rows)
            setNeedSearch(false)
        })
    }
    function changePageNum(num:Number){
        let obj = {
            ...formItem,
            page:num
        }
        setFormItem(obj)
        setNeedSearch(true)
    }
    function searchItem(val:any){
        console.log(val)
        let obj = {
            ...formItem,
            queryStartDate:val['datepicker'][0],
            queryEndDate:val['datepicker'][1],
            activityName:val.activityName
        }
        setFormItem(obj)
        setNeedSearch(true)
    }
    function createActivity(){
        history.push({
            pathname:'/index/create'
        })
    }
  return (
    <div>
        <TableSearch searchItem={searchItem}></TableSearch>
            <div className='createActivity'>
                <Button onClick={createActivity}>+创建活动</Button>
            </div>
        <Table tableData={data} state={props.state} total={total} changePageNum={changePageNum}></Table>
    </div>
  )
}
